package com.entrogy.dao;

import com.entrogy.bean.StudentEntity;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface LeaveMapper {

	@Select("<script>"
			+ "select count(leave_id) from leave_record where teacher_id=#{uid} "
			+ "<if test='selectType == 1'>"
			+ " and isauthority=0"
			+ "</if>"
			+ "<if test='selectType == 2'>"
			+ " and (isauthority=1 or isauthority=2)"
			+ "</if>"
			+ "</script>")
	Integer leaveRevodeCount(@Param("uid") String uid, @Param("selectType") Integer selectType);

	@Select("<script>"
			+ "select * from leave_record where teacher_id=#{uid} "
            + "<if test='selectType == 1'>"
            + " and isauthority=0"
            + "</if>"
			+ "<if test='selectType == 2'>"
            + " and (isauthority=1 or isauthority=2)"
            + "</if>"
            + " order by start_time DESC limit #{beginIndex},#{pageSize}"
			+ "</script>")
	List<Map<String,Object>> getLeaveRecordBean(@Param("uid") String uid, @Param("beginIndex") int beginIndex, @Param("pageSize") int pageSize, @Param("selectType") Integer selectType);

	@Select("select student_name from student_information where id=#{id}")
	String getStuName(@Param("id")int  id);

	@Select("select name from user where id = #{tid}")
	String userTeacherName(@Param("tid") String tid);

	@Select("select parent_visitor_name from parent_visitor_information where parent_visitor_id = #{pid}")
	String userParentName(@Param("pid") String pid);

	@Select("<script>"
			+ "select count(*) from leave_record where parent_id = #{uid} "
			+ "<if test='selectType == 1'>"
			+ " and isauthority=0"
			+ "</if>"
			+ "<if test='selectType == 2'>"
			+ " and (isauthority=1 or isauthority=2)"
			+ "</if>"
			+ "</script>")
	Long leaveRevodeCountByParnet(@Param("uid") String uid, @Param("selectType") Integer selectType);

	@Select("<script>"
			+ "select * from leave_record where parent_id = #{uid} "
			+ "<if test='selectType == 1'>"
			+ " and isauthority=0"
			+ "</if>"
			+ "<if test='selectType == 2'>"
			+ " and (isauthority=1 or isauthority=2)"
			+ "</if>"
			+ " order by start_time DESC limit #{beginIndex},#{pageSize}"
			+ "</script>")
	List<Map<String,Object>> getLeaveRecordBeanByParent(@Param("uid") String uid, @Param("beginIndex") int beginIndex, @Param("pageSize") int pageSize, @Param(value = "selectType") Integer selectType);

	@Select("select * from student_information where (parent1_id=#{parentId} or parent2_id=#{parentId} ) and student_name=#{stuName}")
	StudentEntity getStuKeyInfo(@Param("parentId") String parentId, @Param("stuName") String stuName);

	@Select("SELECT u.id AS teacher_id FROM `user` u LEFT JOIN  position_information p ON u.`pId`= p.`id` inner join student_information si on u.class_id=si.course_id and u.school_code=si.school_code WHERE p.`sid`= 16 AND si.id=#{id}")  //TODO XLR  班主任的判断问题
	String getBanzhuren(int id);

	@Insert("insert into leave_record (parent_id, student_id, teacher_id, leave_time, start_time, end_time, leave_content, isauthority, pass_times) values " +
			"(#{uid}, #{id}, #{teacherId}, now(), #{sTime}, #{eTime}, #{content},0,1)")
	void insertIntoLeaveRecord(@Param("uid") String uid,@Param("id") int id,@Param("teacherId") String teacherId,@Param("sTime") String s_time, @Param("eTime") String e_time,@Param("content") String content);

	@Update("update leave_record set isauthority = #{deal} where leave_id = #{listId}")
	void updateLeaveRecord(@Param("listId") String listId, @Param("deal") int deal);

	@Select("select parent_id from leave_record where leave_id = #{listId}")
	String getParentId(@Param("listId") String listId);

}
